#include "CollectAllPCResultCmd.h"
#include <conio.h>
#include <wmi_constants.h>

using namespace std;

void CollectAllPCResultCmd::execute()
{
	wstring user(L"Administrator");
	wstring password(L"admin");
	wstring domain(L"venus.local");
	wstring serverName(L"VenusServer");

	// wcin >> user >> password >> domain ;

	vector<ADCM_Computer> computers;
	bridget_communicator::getComputers(
		serverName,
		domain,
		wstring(L""),
		user,
		password,
		computers);

	// TODO: VALIDATE return
	bridget_communicator::save(serverName,domain,computers);

	VNC_ComputerThread** pcThread =  new VNC_ComputerThread*[computers.size()];

	vector<ADCM_Computer>::iterator iter;
	int index = 0;
	
	ResultFormatter resFormatter;



	wstring emptyString (L"");

	for (iter = computers.begin(); iter != computers.end(); iter++)
	{				
		if((*iter).getCn().compare(L"JURENA-47009005") == 0){
		if (isLocalComputer((*iter).getCn()))
			pcThread[index] = new VNC_ComputerThread(resFormatter,*iter, emptyString, emptyString, emptyString,true);
		else pcThread[index] = new VNC_ComputerThread(resFormatter,*iter, user, password, domain,false);

		pcThread[index++]->Start();
		}
	}	

	while(getch() != 'q');
}

bool CollectAllPCResultCmd::isLocalComputer(const wstring& pcName)
{
	bool res = false;

	const long INFO_BUFFER_SIZE = 100;

	TCHAR infoBuf[INFO_BUFFER_SIZE];
	DWORD bufCharCount = INFO_BUFFER_SIZE;

	GetComputerName(infoBuf, &bufCharCount);

	wstringstream a;
	a << infoBuf;

	wstring var(a.str().c_str());

	if(wcscmp(pcName.c_str(), var.c_str()) == 0)
		res = true;
	
	return res;
}